<template>
	<view class="nvue-page-root">
		<view class="uni-form-item uni-column">
			<view class="title"><text class="uni-form-item__title">学校名称</text></view>
			<view class="uni-input-wrapper">
				<input class="uni-input" maxlength="20" placeholder="请输入学校名称" v-model="schoolName" />
			</view>
		</view>
		<view class="uni-padding-wrap uni-common-mt">
			<view class="button-sp-area">
				<button type="default" @click="search()">搜索</button>
				<navigator url="./schoolAdd"><button type="primary">添加</button></navigator>
				<button type="default" @click="DownFile(0)">根据条件下载</button>
				<button type="default" @click="DownFile(1)">根据选中下载</button>
				<!-- #ifdef H5 -->
				<button type="default" @click="UploadSchool(1)">导入数据</button>
				<!-- #endif -->
				<button type="default" @click="SchoolDels(1)">批量删除</button>
				<navigator url="./schoolFile"><button type="primary">跳转上传</button></navigator>
				<navigator url="../schoollist/index"><button type="primary">跳转测试</button></navigator>
			</view>
		</view>
		<view>
			<checkbox-group @change="checkboxChange">
				<view v-for="(item,index) in dataSchoolList">
					<label>
						<view>
							<checkbox :value="item.ID" :checked="schoolIds.some(it=>it==item.ID)" />
						</view>
						<view @click="schoolEdit(item.ID)">
							<view>
								<text>({{item.SchoolCode}}){{item.SchoolName}}</text>
								<text style="float: right;">{{item.SchoolType}}</text>
							</view>
							<view>

								<text style="height: 20rpx;">{{item.Remark}}</text>
								<button style="float: right;" type="default" size="mini"
									@tap.stop="SchoolDel(item.ID,index)">删除</button>
							</view>
						</view>
					</label>
				</view>
			</checkbox-group>
		</view>
	</view>
</template>

<script>
	import schoolservice from '../../myservice/schoolservice.js'
	import commonservice from '../../myservice/commonservice.js'
	import fileservice from '../../myservice/fileservice.js'

	export default {
		data() {
			return {
				schoolName: "",
				dataSchoolList: [],
				schoolIds: [],
				PageIndex: 1
			}
		},
		onPullDownRefresh() {
			uni.showToast({
				title: '下拉刷新'
			});
			this.PageIndex=1;
			this.dataSchoolList = [];
			setTimeout(function() {
				uni.stopPullDownRefresh();
			}, 1000);
		},
		onReachBottom() { //触底加载分页
			uni.showToast({
				title: '触底加载分页'+this.PageIndex
			});
			this.getDateList(this.schoolName);
		},
		methods: {
			search() {
				this.PageIndex=1;
				this.dataSchoolList = [];
				this.getDateList(this.schoolName);
				// var that = this;
				// console.log(that.schoolName);
				// schoolservice.SchoolSearch({
				// 	"SchoolName": that.schoolName
				// }).then(res => {
				// 	console.log(JSON.stringify(res.data))
				// 	that.dataSchoolList = res.data.Data;
				// })
			},
			getDateList(title) {
				var that = this;
				schoolservice.SchoolSearch({
					"Page": that.PageIndex,
					"Limit": 10,
					"SchoolName": title
				}).then(res => {
					console.log(JSON.stringify(res.data))
					if (res.data.Data.length > 0) {
						that.PageIndex++;
						that.dataSchoolList=that.dataSchoolList.concat(res.data.Data);
					}
				})
			},
			schoolEdit: function(schoolId) {
				uni.navigateTo({
					url: "./schoolAdd?schoolId=" + schoolId
				})
			},
			SchoolDel: function(schoolId, index) { //删除学校信息
				console.log(schoolId)
				var that = this;
				uni.showModal({
					title: '提示',
					content: '是否确认删除该学校信息',
					showCancel: true,
					//cancelText: '',
					//confirmText: '',
					success: res => {
						if (res.confirm) {
							schoolservice.SchoolDel(schoolId).then(res => {
								console.log(JSON.stringify(res));
								if (res.statusCode == 200) {
									uni.showToast({
										title: '删除成功'
									});
									// 从索引处删除一个元素
									that.dataSchoolList.splice(index, 1);
									//var arr = Array.from(that.dataSchoolList);
									//arr.splice(index,1);
								}
							})
						} else {
							uni.showToast({
								title: '取消'
							})
						}
					},
					fail: () => {},
					complete: () => {}
				});
			},
			SchoolDels: function() {
				var that = this;
				schoolservice.SchoolDels(that.schoolIds).then(res => {
					console.log(JSON.stringify(res));
					if (res.statusCode == 200) {
						uni.showToast({
							title: '删除成功'
						});
						that.dataSchoolList = that.dataSchoolList.filter(function(currentValue, index) {
							return !that.schoolIds.some(item => item == currentValue.ID);
						})

						// 从索引处删除一个元素
						//that.dataSchoolList.splice(index, 1);
						//var arr = Array.from(that.dataSchoolList);
						//arr.splice(index,1);
					}
				})
			},
			DownFile: function(type) {
				//type:0:条件下载 1：选中下载
				var that = this;
				if (type == 0) {
					schoolservice.ExportExcel({
						"SchoolName": that.schoolName
					}).then(res => {
						console.log(JSON.stringify(res))
					})
				} else {
					schoolservice.ExportExcelByIds(that.schoolIds).then(res => {
						console.log(JSON.stringify(res))
					})
				}
			},
			checkboxChange: function(e) {
				console.log(JSON.stringify(e))
				this.schoolIds = e.detail.value;
			},
			UploadSchool: function() {
				uni.chooseFile({
					count: 1,
					type: 'all',
					success: function(res) {
						console.log(res.tempFilePaths[0])
						fileservice.uploadFile(res.tempFilePaths[0]).then(re => { //文件上传服务器
							console.log(JSON.stringify(re))
							if (re.statusCode == 200) {
								var data = JSON.parse(re.data);
								var id = data.Id;
								console.log(id)
								schoolservice.SchoolImprot({
									UploadFileId: id,
									// UseBulkSave:false,
									// IsOverWriteExistData:false
								}).then(r => {
									console.log(JSON.stringify(r))
									if (r.statusCode == 200) {
										uni.showToast({
											title: '成功导入' + r.data + '条',
											icon: 'none'
										});
									}
								})
							} else {
								uni.showToast({
									title: re.errMsg
								});
							}
						})
					},
					fail: function(ex) {
						uni.showToast({
							title: '选择图片失败'
						});
					}
				})
			}
		},
		onShow() {
			this.dataSchoolList = [];
			this.PageIndex=1;
			if (commonservice.AccessToken == null || commonservice.AccessToken == '') {
				uni.navigateTo({
					url: "../login/login"
				})
			}
		}
	}
</script>

<style>
</style>
